package com.es.hr.baoxiaoShenpi;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.es.common.CommonBean;
import com.es.common.CommonBiz;
import com.es.hr.baoxiao.BaoxiaoBean;
import com.es.hr.baoxiao.BaoxiaoBiz;
import com.es.hr.manage.ManageBiz;
import com.es.hr.shop.ShopBiz;
import com.es.main.LoginUserBeen;
import com.es.util.excel.BaoxiaoHistoryExcelOrderBean;
import com.es.util.excel.EmpExcelOrderBean;
import com.es.util.excel.ExportBaoxiaoExcelJsp;
import com.es.util.excel.ExportExcelJsp;
import com.es.util.excel.ShopExcelOrderBean;
import com.es.util.page.PageBean;
import com.opensymphony.webwork.interceptor.ServletRequestAware;
import com.opensymphony.webwork.interceptor.ServletResponseAware;
import com.opensymphony.xwork.ActionSupport;

public class ExportBaoxiaoHistoryExcelAction  extends ActionSupport implements ServletRequestAware, ServletResponseAware{
	
	  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	HttpServletRequest request;
	 HttpServletResponse response;

	  @SuppressWarnings("unchecked")
	public String execute() throws Exception
	  {
		  request.setCharacterEncoding("GB2312");
		  String result = "";
		  
		  LoginUserBeen userLogin = (LoginUserBeen)request.getSession().getAttribute("loginuser");
			String role = userLogin.getLoginRole();
			
			String area_code = this.request.getParameter("emp_area_code");
			this.request.setAttribute("emp_area_code", area_code);
			
			String province_code = this.request.getParameter("emp_province_code");
			String city_code = this.request.getParameter("emp_city_code");
			
			String rtm_code = this.request.getParameter("rtm_code");
			String rtm_name = this.request.getParameter("rtm_name");
			String ctm_code = this.request.getParameter("ctm_code");
			String ctm_name = this.request.getParameter("ctm_name");
			
			String baoxiao_starttime = this.request.getParameter("baoxiao_starttime").trim();
			if(baoxiao_starttime.length() > 0) baoxiao_starttime += "-01";
			String baoxiao_endtime = this.request.getParameter("baoxiao_endtime").trim();
			if(baoxiao_endtime.length() > 0) baoxiao_endtime += "-32";
			
			String baoxiao_state = this.request.getParameter("baoxiao_state");
				   
			CommonBean rtmrole = new CommonBiz().getCommonByTypeAndCode("role", "RTM");
			CommonBean shiyebu = new CommonBiz().getCommonByTypeAndCode("shiyebu", "ITMall");
			CommonBean shiyeburole = new CommonBiz().getCommonByTypeAndCode("role", "shiyebu");
			
			
			BaoxiaoBiz baoxiaoBiz = new BaoxiaoBiz();
			BaoxiaoBean bean = baoxiaoBiz.getBaoxiaoBaseInfoFromEmp(userLogin.getLoginId());
			BaoxiaoShenpiBiz baoxiaoShenpiBiz = new BaoxiaoShenpiBiz();
			ArrayList<HashMap<String, String>> list = null;
			if(role.equals(rtmrole.getCommonValue()) && userLogin.getShiyebu().equals(shiyebu.getCommonValue())){

			list = baoxiaoShenpiBiz .searchBaoxiaoShenpiHistoryForExport(area_code,province_code,city_code, bean.getCtm_code(), bean.getCtm_name(), ctm_code,ctm_name, baoxiao_state, baoxiao_starttime, baoxiao_endtime,false);
			}else if(role.equals("1") || role.equals("zongbu") || (role.equals(shiyeburole.getCommonValue())) && userLogin.getShiyebu().equals(shiyebu.getCommonValue())){

				list = baoxiaoShenpiBiz .searchBaoxiaoShenpiHistoryForExport(area_code,province_code,city_code, "", rtm_name, ctm_code,ctm_name, baoxiao_state, baoxiao_starttime, baoxiao_endtime,true);

			}

		 
		   String excelString = ExportBaoxiaoExcelJsp.getExcelJspString(list,BaoxiaoHistoryExcelOrderBean.getBaoxiaoMap());
		  
		  //String filename="测试";
		  excelString = "<html><head><title></title></head><body > "+excelString+"</body></html>";
		  response.setContentType("application/vnd.ms-excel;charset=gb2312");
		  //response.setHeader("Content-disposition","attachment; filename="+new String(filename.getBytes("gbk"),"iso8859-1")+".xls" );
		  response.getWriter().write(excelString);
	      return "";
	  }

	  public void setServletRequest(HttpServletRequest request) {
	    this.request = request;
	  }

	  public void setServletResponse(HttpServletResponse response) {
	    this.response = response;
	  }
	}
